//
//  XDGetCityViewController.m
//  xindailianmeng
//
//  Created by 张东 on 2017/3/8.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDGetCityViewController.h"
#import "XDGetCityHeaderView.h"
#import "AFHTTPSessionManager+POST.h"
@interface XDGetCityViewController ()<UISearchBarDelegate,UITableViewDelegate, UITableViewDataSource,XDGetCityHeaderViewDelegate>
@property (nonatomic, weak) UISearchBar *searchBar;

@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, copy) NSString *moreCityStr;


@property (nonatomic, strong) NSArray *cityArr;
@end

@implementation XDGetCityViewController

- (NSArray *)cityArr {
    if (_cityArr == nil) {
        _cityArr = [NSArray array];
    }
    return _cityArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现";
    self.view.backgroundColor = kColorRGB(234, 234, 234);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"提现须知"  style:UIBarButtonItemStylePlain target:self action:@selector(rightItem)];
    rightItem.tintColor = kColorRGB(187,147,115);
    self.navigationItem.rightBarButtonItem = rightItem;
    [self setupUI];
}

- (void)setupUI {
    
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 43)];
    searchBar.placeholder = @"请输入银行卡开卡银行";
    searchBar.delegate = self;
    searchBar.backgroundColor = kColorRGB(234, 234, 234);
    self.searchBar = searchBar;
    [self.view addSubview:searchBar];
    
    XDGetCityHeaderView *getCityView = [[XDGetCityHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 399)];
    getCityView.delegate = self;
    UITableView *tablew = [[UITableView alloc] initWithFrame:CGRectMake(0,43, kScreenW, kScreenH - 43 - 64)];
    tablew.delegate = self;
    tablew.dataSource = self;
    tablew.showsVerticalScrollIndicator = NO;
    tablew.showsHorizontalScrollIndicator = NO;
    self.tableView = tablew;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [self.view addSubview:tablew];
    tablew.backgroundColor = kColorRGB(234, 234, 234);
    tablew.tableHeaderView = getCityView;
    tablew.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 10)];
    tablew.tableFooterView = view;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cityArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId = @"getCityCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        
    }
    cell.textLabel.text = self.cityArr[indexPath.row][@"city"];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hotCityArr" object:nil userInfo:self.cityArr[indexPath.row]];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 20)];
    view.backgroundColor = kColorRGB(234, 234, 234);
    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kScreenW - 15, 20)];
    if (self.cityArr.count) {
        
        lb.text = self.moreCityStr;
    } else {
        lb.text = @"无搜索结果";
    }
    lb.textColor = kColorRGB(187,147,115);
    lb.textAlignment = NSTextAlignmentLeft;
    lb.font = kBaseFont_(13);
    [view addSubview:lb];
    return view;
}
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    [self.tableView setContentOffset:CGPointMake(0, 399) animated:YES];
    return YES;
    
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(nonnull NSString *)searchText {
    NSString *searchBarText = [[self.searchBar.text stringByReplacingOccurrencesOfString:@" " withString:@""] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    [self searchCityLoadData:searchBarText];
}

- (void)hotCityClickWithXDGetCityHeaderView:(XDGetCityHeaderView *)getCityHeaderView hotCityDic:(NSDictionary *)hotCityDic {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hotCityArr" object:nil userInfo:hotCityDic];
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)moreCityClickWithXDGetCityHeaderView:(XDGetCityHeaderView *)getCityHeaderView moreCityStr:(NSString *)moreCityStr {
    [self loadMoreCityData:moreCityStr];
    
}

- (void)loadMoreCityData: (NSString *)moreCityStr {
    
    NSDictionary * param = @{@"method":@"getCity",@"initialCode":moreCityStr};
    
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"100000"]) {

            /*
             
             "type": "success",
             "code": "100000",
             "message": "操作成功",
             "result": {
             "bfCityList": [
             {
             "province": "陕西省",
             "city": "宝鸡市",
             "cityCode": "7930"
             },
             {
             "province": "陕西省",
             "city": "宝鸡市凤翔县",
             "cityCode": "7932"
             },
             {
             "province": "陕西省",
             "city": "宝鸡市扶风县",
             "cityCode": "7934"
             },
             {
             "province": "陕西省",
             "city": "宝鸡市眉县",
             "cityCode": "7935"
             },
             {
             "province": "陕西省",
             "city": "宝鸡市岐山县",
             "cityCode": "7933"
             },
             {
             "province": "贵州省",
             "city": "毕节市",
             "cityCode": "7090"
             },
             {
             "province": "北京市",
             "city": "北京市",
             "cityCode": "1000"
             }
             ]
             }
             }
             张路路  14:44:02
             getCity
             */
            self.moreCityStr = moreCityStr;
            
            self.cityArr = responseObject[@"result"][@"bfCityList"];
            [self.tableView reloadData];
            [self.tableView setContentOffset:CGPointMake(0, 399) animated:NO];
        } else if ([responseObject[@"code"] isEqualToString:@"100001"]){ // 超时
            
            
        } else {
            
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}


- (void)searchCityLoadData:(NSString *)searchBarText{
     NSString *searchStr = [self.searchBar.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    [self loadMoreCityData:searchStr];
}

- (void)rightItem {
    
    NSLog(@"提现须知");
    
}
@end
